package com.shengqi.xinnongren.work.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.shengqi.xinnongren.R;

import java.util.List;


public class SpecificationDialog extends Dialog {
    public SpecificationDialog(@NonNull Context context) {
        super(context);
    }

    public SpecificationDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
    }

    public static class Builder {

        private Context mContext;
        private List<String> mDataList;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setDataList(List<String> stringList) {
            this.mDataList = stringList;
            return this;
        }

        public SpecificationDialog build() {
            if (mDataList == null)
                throw new RuntimeException("数据列表不能为空");

            final SpecificationDialog dialog = new SpecificationDialog(mContext, R.style.CustomDialog);
            View layout = View.inflate(mContext, R.layout.dialog_specification, null);

            RecyclerView recyclerView = layout.findViewById(R.id.rv_list);
            recyclerView.setAdapter(new PAdapter());
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            recyclerView.setLayoutManager(layoutManager);

            layout.findViewById(R.id.tv_done).setOnClickListener(v -> dialog.dismiss());

            View indicator = layout.findViewById(R.id.v_indicator);
//            indicator.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                @Override
//                public void onGlobalLayout() {
//                    int height2 = mDataList.size() * layoutManager.findViewByPosition(layoutManager.findFirstVisibleItemPosition()).getHeight();
//                    if (height2 < recyclerView.getHeight()) {
                        ((ViewGroup) indicator.getParent()).setVisibility(View.GONE);
//                        return;
//                    }
//                    ViewGroup.LayoutParams layoutParams = indicator.getLayoutParams();
//                    layoutParams.height = (int) (((ViewGroup) indicator.getParent()).getHeight() * (recyclerView.getHeight() * 1f / height2));
//                    indicator.setLayoutParams(layoutParams);
//                    indicator.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                }
//            });

//            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//                @Override
//                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
////                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
////                    int visibleNum = lastVisibleItemPosition - firstVisibleItemPosition;
////                    if (layoutManager.findViewByPosition(firstVisibleItemPosition).getY() < 0) {
////                        visibleNum++;
////                    }
////                    float per = layoutManager.findFirstVisibleItemPosition() * 1f / (LIST_NUM - visibleNum);
//                    int maxDistance = ((ViewGroup) indicator.getParent()).getHeight() - indicator.getHeight();
////                    LogUtil.e(String.valueOf(per));
////                    indicator.setTranslationY(maxDistance * per);
//                    View view = layoutManager.findViewByPosition(firstVisibleItemPosition);
//                    int height = mDataList.size() * view.getHeight() - recyclerView.getHeight();
//
//                    float scrollY = firstVisibleItemPosition * view.getHeight() - view.getTop();
//                    indicator.setTranslationY(maxDistance * (scrollY / height));
//                    LogUtil.e(String.format("height=%s , scrollY=%s , v.top=%s , firstPosition=%s", height, scrollY, view.getTop(), firstVisibleItemPosition));
//
//                }
//            });

            //将布局添加到 dialog 中
            dialog.setContentView(layout);
            return dialog;
        }

        private class PAdapter extends RecyclerView.Adapter<PAdapter.Holder> {
            @NonNull
            @Override
            public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_goods_specification, parent, false));
            }

            @Override
            public void onBindViewHolder(@NonNull Holder holder, int position) {
                String[] data = mDataList.get(position).split("_data_");
                if (data.length > 0)
                    holder.tvTitle.setText(data[0]);
                if (data.length > 1)
                    holder.tvParam.setText(data[1]);
            }

            @Override
            public int getItemCount() {
                return mDataList.size();
            }

            public class Holder extends RecyclerView.ViewHolder {
                TextView tvTitle;
                TextView tvParam;

                public Holder(View itemView) {
                    super(itemView);
                    tvTitle = itemView.findViewById(R.id.tv_title);
                    tvParam = itemView.findViewById(R.id.tv_param);
                }
            }
        }
    }
}
